package com.yupi.oj.model.dto.question;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.yupi.oj.model.entity.Question;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.List;

@Data
@ApiModel("题目修改请求")
public class QuestionEditRequest implements Serializable {

    @ApiModelProperty("题目 ID")
    private Long id;

    @ApiModelProperty(value = "题目标题")
    private String title;

    @ApiModelProperty(value = "题目描述")
    private String description;

    @ApiModelProperty(value = "输入")
    private String input;

    @ApiModelProperty(value = "输出")
    private String output;

    @ApiModelProperty(value = "样例")
    private List<ExampleDTO> exampleDTOList;

    @ApiModelProperty(value = "提示")
    private String tip;

    @ApiModelProperty(value = "难度等级")
    private String difficultLevel;

    @ApiModelProperty(value = "标签（数组）")
    private List<String> tagList;

    @ApiModelProperty(value = "内存限制（KB）")
    private Integer memoryLimit;

    @ApiModelProperty(value = "时间限制（ms）")
    private Integer timeLimit;

    private static final long serialVersionUID = 1L;

    /**
     * pojo to bean
     *
     * @param questionEditRequest 修改请求数据
     * @param question            问题数据
     */
    public static void pojoToBean(QuestionEditRequest questionEditRequest, Question question) {
        List<ExampleDTO> exampleDTOList = questionEditRequest.getExampleDTOList();
        List<String> tagList = questionEditRequest.getTagList();
        BeanUtils.copyProperties(questionEditRequest, question);

        if (ObjectUtil.isNotEmpty(exampleDTOList)) {
            question.setExample(JSONUtil.toJsonStr(exampleDTOList));
        }
        if (ObjectUtil.isNotEmpty(tagList)) {
            question.setTag(JSONUtil.toJsonStr(tagList));
        }
    }
}